package com.ysd.lis.service.bac.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.bac.BacAntibioticsDict;
import com.ysd.lis.entity.bac.BacDrugResultTempdetail;
import com.ysd.lis.mapper.bac.BacDrugResultTempdetailMapper;
import com.ysd.lis.service.bac.BacDrugResultTempdetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cfz
 * @since 2024-03-28
 */
@Service
public class BacDrugResultTempdetailServiceImpl extends ServiceImpl<BacDrugResultTempdetailMapper, BacDrugResultTempdetail> implements BacDrugResultTempdetailService {
    @Autowired
    BacDrugResultTempdetailMapper bacDrugResultTempdetailMapper;

    @Override
    public Result findBacDrugResultTempdetailList(BacDrugResultTempdetail bacDrugResultTempdetail) {
        List<BacDrugResultTempdetail> list = findBacDrugResultTempdetailArray(bacDrugResultTempdetail);
        return Result.succ(1, "请求成功", list);
    }

    @Override
    public List<BacDrugResultTempdetail> findBacDrugResultTempdetailArray(BacDrugResultTempdetail bacDrugResultTempdetail) {
        MPJLambdaWrapper<BacDrugResultTempdetail> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(BacDrugResultTempdetail.class);
        queryWrapper.selectAs("ant", BacAntibioticsDict::getVerificationPrice, BacDrugResultTempdetail::getVerificationPrice);
        queryWrapper.eq(BacDrugResultTempdetail::getDelFlag, 0);
        //queryWrapper.eq(StringUtils.isNotBlank(bacDrugResultTempdetail.getTempldateNo()), BacDrugResultTempdetail::getTempldateNo, bacDrugResultTempdetail.getTempldateNo());
        queryWrapper.eq(StringUtils.isNotBlank(bacDrugResultTempdetail.getTempldateNo()), BacDrugResultTempdetail::getTempldateNo, bacDrugResultTempdetail.getTempldateNo());
        queryWrapper.eq(StringUtils.isNotBlank(bacDrugResultTempdetail.getTempldateNa()), BacDrugResultTempdetail::getTempldateNa, bacDrugResultTempdetail.getTempldateNa());

        //queryWrapper.eq(StringUtils.isNotBlank(bacDrugResultTempdetail.getAntId()), BacDrugResultTempdetail::getAntId, bacDrugResultTempdetail.getAntId());
        queryWrapper.eq(StringUtils.isNotBlank(bacDrugResultTempdetail.getAntNo()), BacDrugResultTempdetail::getAntNo, bacDrugResultTempdetail.getAntNo());
        queryWrapper.eq(StringUtils.isNotBlank(bacDrugResultTempdetail.getAntNa()), BacDrugResultTempdetail::getAntNa, bacDrugResultTempdetail.getAntNa());

        //queryWrapper.eq(StringUtils.isNotBlank(bacDrugResultTempdetail.getAstTestMthDictId()), BacDrugResultTempdetail::getAstTestMthDictId, bacDrugResultTempdetail.getAstTestMthDictId());
        queryWrapper.eq(StringUtils.isNotBlank(bacDrugResultTempdetail.getAstTestMthDictNo()), BacDrugResultTempdetail::getAstTestMthDictNo, bacDrugResultTempdetail.getAstTestMthDictNo());
        queryWrapper.eq(StringUtils.isNotBlank(bacDrugResultTempdetail.getAstTestMthDictNa()), BacDrugResultTempdetail::getAstTestMthDictNa, bacDrugResultTempdetail.getAstTestMthDictNa());

        //queryWrapper.eq(StringUtils.isNotBlank(bacDrugResultTempdetail.getAntPropGroupDictId()), BacDrugResultTempdetail::getAntPropGroupDictId, bacDrugResultTempdetail.getAntPropGroupDictId());
        queryWrapper.eq(StringUtils.isNotBlank(bacDrugResultTempdetail.getAntPropGroupDictNo()), BacDrugResultTempdetail::getAntPropGroupDictNo, bacDrugResultTempdetail.getAntPropGroupDictNo());
        queryWrapper.eq(StringUtils.isNotBlank(bacDrugResultTempdetail.getAntPropGroupDictNa()), BacDrugResultTempdetail::getAntPropGroupDictNa, bacDrugResultTempdetail.getAntPropGroupDictNa());

        //queryWrapper.eq(StringUtils.isNotBlank(bacDrugResultTempdetail.getInterCodeDictId()), BacDrugResultTempdetail::getInterCodeDictId, bacDrugResultTempdetail.getInterCodeDictId());
        queryWrapper.eq(StringUtils.isNotBlank(bacDrugResultTempdetail.getInterCodeDictNo()), BacDrugResultTempdetail::getInterCodeDictNo, bacDrugResultTempdetail.getInterCodeDictNo());
        queryWrapper.eq(StringUtils.isNotBlank(bacDrugResultTempdetail.getInterCodeDictNa()), BacDrugResultTempdetail::getInterCodeDictNa, bacDrugResultTempdetail.getInterCodeDictNa());

        queryWrapper.leftJoin(BacAntibioticsDict.class, "ant", p -> p.eq(BacAntibioticsDict::getAntCode, BacDrugResultTempdetail::getAntNo).isNotNull(BacAntibioticsDict::getAntCode).ne(BacAntibioticsDict::getAntCode, ""));

        queryWrapper.orderByAsc(BacDrugResultTempdetail::getSeq);
        return bacDrugResultTempdetailMapper.selectList(queryWrapper);
    }

    @Override
    public Result addBacDrugResultTempdetail(BacDrugResultTempdetail bacDrugResultTempdetail) {
        int i = bacDrugResultTempdetailMapper.insert(bacDrugResultTempdetail);
        if (i > 0) {
            return Result.succ(1, "新增成功", bacDrugResultTempdetail);
        }
        return Result.fail("新增失败");
    }

    @Override
    public Result deleteBacDrugResultTempdetail(BacDrugResultTempdetail bacDrugResultTempdetail) {
        bacDrugResultTempdetail.setDelFlag(1);
        int i = bacDrugResultTempdetailMapper.updateById(bacDrugResultTempdetail);
        if (i > 0) {
            return Result.succ(1, "删除成功", bacDrugResultTempdetail);
        }
        return Result.fail("删除失败");
    }

    @Override
    public Result updateBacDrugResultTempdetail(BacDrugResultTempdetail bacDrugResultTempdetail) {
        int i = bacDrugResultTempdetailMapper.updateById(bacDrugResultTempdetail);
        if (i > 0) {
            return Result.succ(1, "更新成功", bacDrugResultTempdetail);
        }
        return Result.fail("更新失败");
    }

    @Override
    public Result BatchSaveBacDrugResultTempdetail(List<BacDrugResultTempdetail> bacDrugResultTempdetails) {
        if (bacDrugResultTempdetails.isEmpty()) {
            return Result.fail("没有要更新的项");
        }
        List<BacDrugResultTempdetail> updateList = bacDrugResultTempdetails.stream().filter(p -> StringUtils.isNotBlank(p.getId())).collect(Collectors.toList());
        if (!updateList.isEmpty()) {
            //取出Id集合
            List<String> idList = updateList.stream().map(p -> p.getId()).collect(Collectors.toList());
            bacDrugResultTempdetailMapper.deleteBatchIds(idList);
        }
       // List<BacDrugResultTempdetail> nList = bacDrugResultTempdetails.stream().filter(p -> StringUtils.isBlank(p.getId())).collect(Collectors.toList());
        if (!bacDrugResultTempdetails.isEmpty()) {
            boolean r = this.saveBatch(bacDrugResultTempdetails);
        }

        return Result.succ(1, "操作成功", bacDrugResultTempdetails);
    }

    @Override
    public Result BatchDeleteBacDrugResultTempdetail(List<BacDrugResultTempdetail> bacDrugResultTempdetails) {
        for (BacDrugResultTempdetail m : bacDrugResultTempdetails) {
            m.setDelFlag(1);
        }
        boolean r = this.updateBatchById(bacDrugResultTempdetails);
        return r ? Result.succ(1, "删除成功", bacDrugResultTempdetails) : Result.fail("删除失败");
    }

}
